import collections
from collections import Counter


class UsefulCode:
    def sliceUse(self):
        # 切片反转字符串
        mystring = 'ABCDEFG'
        reversed_string = mystring[::-1]
        print(reversed_string)

    def titleCase(self):
        # 首字母大写
        my_string = "my name is chaitanya baweja"
        new_string = my_string.title()
        print(new_string)

    def clearRepeat(self):
        # 去重字符串
        my_string = "aavvccccddddeee"
        temp_set = set(my_string)
        new_string = ''
        for i in temp_set:
            new_string += i
        print(new_string)

    def countNum(self):
        # 统计次数
        my_list = [1, 1, 1, 1, 2, 3, 2, 3, 4, 5, 6, 6]
        count = Counter(my_list)
        print(count)
        print(count.most_common(2))

    def indexValue(self):
        # 快速获取列表索引和值的方法
        my_list = [1, 1, 1, 1, 2, 3, 2, 3, 4, 5, 6, 6]
        for index, value in enumerate(my_list):
            print(index, value)

    def countWord(self):
        content = "A,B,A"
        list_content = content.split(' ')
        print(list_content)
        count = Counter(list_content)
        print(count.most_common(2))

    def generate(self, numRows):
        '''两个数组嵌套'''
        ret = list()
        for i in range(numRows):
            print("i的值是", i)
            row = list()
            for j in range(0, i + 1):
                print("j的值是", j)
                if j == 0 or j == i:
                    row.append(1)
                else:
                    row.append(ret[i - 1][j] + ret[i - 1][j - 1])
                print('row的值是：', row)
            ret.append(row)
        return ret

    def isPalindrome(self, s):
        # new_string = s.lower().split(' ')
        # print(new_string)

        s = list(s)
        new_string = []
        for i in s:
            if i >= 'a' and i <= 'z' or i >= 'A' and i <= 'Z' or i >= '0' and i <= '9':
                new_string.append(i.lower())
        print(new_string)
        new_s = ''
        for i in new_string:
            new_s += i
        print(new_s)
        if new_s == new_s[::-1]:
            return True
        else:
            return False

    def majorityElement(self, nums):
        '''
        1、key后面是函数
        2、max遍历dict的值，取最大
        3、key后面的函数通过这个值去查找对应的键
        4、不加key这个函数的话，默认遍历的是字典的key，最后输出最大的键
        '''
        n = collections.Counter(nums)
        print(n)
        for i in n.keys():
            print(i)
        aa = n.get
        print(aa)
        print(max(n.keys(), key=n.get))

    def titleToNumber(self, columnTitle):
        ans = 0
        base = ord('A') - 1
        print(base)
        for ch in columnTitle:
            print(ord(ch))
            ans = ans * 26 + (ord(ch) - base)
            print("当前的ans:", ans)

        return ans

    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        l = len(set(list(ransomNote)))
        num = 0
        n = collections.Counter(list(ransomNote))
        m = collections.Counter(list(magazine))
        for i in n:
            for j in m:
                print(j)
                if i == j and m.get(j) >= n.get(i):
                    num += 1
        if num == l:
            return True
        else:
            return False

    def moveZeroes(self, nums) -> None:
        n = collections.Counter(nums)
        count = n.get(0)
        for i in range(count):
            nums.remove(0)
        nums = list(set(nums))
        for i in range(count):
            nums.append(0)
        print(nums)

    def wordPattern(self, pattern: str, str: str) -> bool:
        res = str.split()
        print(pattern.index)
        for i in map(pattern.index, pattern):
            print(i)
        print(list(map(pattern.index, pattern)))
        return list(map(pattern.index, pattern)) == list(map(res.index, res))

    def firstUniqChar(self, s: str) -> int:
        new_s = Counter(s)
        print(new_s)
        for i in new_s:
            print(i)
            if new_s[i] == 1:
                return s.index(i)
        return -1

    def intersect(self, nums1: [int], nums2: [int]) -> [int]:
        nums1.sort()
        nums2.sort()
        r = []
        left, right = 0, 0
        while left < len(nums1) and right < len(nums2):
            if nums1[left] < nums2[right]:
                left += 1
            elif nums1[left] == nums2[right]:
                r.append(nums1[left])
                left += 1
                right += 1
            else:
                right += 1
        return r

    def thirdMax(self, nums):
        n = set(nums)
        print(len(n))
        new_l = []
        for i in n:
            new_l.append(i)
        new_l.sort()
        if len(new_l) > 2:
            print(new_l[-3])
            return new_l[-3]
        else:
            return new_l[-1]


if __name__ == '__main__':
    a = UsefulCode()
    s = "leetcode"
    nums = [-1, 2, 3,-1]
    a.thirdMax(nums)
